<?php
namespace app\controllers;
use app\models\ApplyModel;
use app\models\CourseModel;
use app\service\DecorationService;
use wlcat\http\controllers\Controller;
class DecorationController extends Controller{
    public function index()
    {
        $this->assignFormToken();
        $this->display();
    }

    public function count()
    {
        $this->checkFormToken();

        $inputs  = $this->checkFormData();

        $split = array_get($inputs, 'split', '*');
        $block = explode("\n", $inputs['arr']);
        $data = [];
        foreach ($block as $line)
        {
            $line = trim($line);
            if (empty($line))
            {
                continue;
            }
            $temp = explode($split, trim($line));
            if (count($temp) != 2)
            {
                return $this->error('这行的数据：'.$line.' 错误');
            }
            $data[] = [
                'width' => (float)$temp[0],
                'height'=> (float)$temp[1]
            ];
        }
        unset($inputs['arr']);
        $inputs['data'] = $data;
        $service = new DecorationService($inputs);

        $this->assign('area', $service->getArea());
        $this->assign('number', $service->getBlock());

        $this->display();
    }


    public function checkFormData()
    {
        $data = $this->request->input('post.');
        $fields = [
            'baseWidth' => [
                'method' => 'float',
                'tip'    => '宽度'
            ],
            'baseHeight' => [
                'method' => 'float',
                'tip'    => '高度',

            ],
            'arr' => [
                'method' => 'string',
                'tip'    => '重量',

            ],
            'split' => [
                'method' => 'string',
                'tip'    => '分隔符',
                'unable' => true,
                'in'     => ['*', ',', '-']
            ]
        ];

        foreach ($fields as $key => $limit)
        {
            if (!isset($data[$key]))
            {
                return $this->error('不存在: '.$limit['tip'], 3);

            }
            $tips = $limit['tip'].'请重新写';

            switch ($limit['method'])
            {
                case 'float':
                    $data[$key] = (float)$data[$key];
                    if (empty($data[$key]) && (!isset($limit['unable']) || $limit['unable'] === false))
                    {
                        return $this->error($tips, 3);
                    }
                    break;
                case 'int':
                    $data[$key] = (int)$data[$key];
                    if (empty($data[$key]))
                    {
                        return $this->error($tips, 3);

                    }
                    break;
                case 'in':
                    if (!in_array($data[$key], $limit['limit']))
                    {
                        return $this->error($tips, 3);
                        die;
                    }
                    break;
                case 'string':
                    $data[$key] = (string)(trim($data[$key]));
                    if (empty($data[$key]))
                    {
                        return $this->error($tips, 3);

                    }
                    break;
            }
        }
        return $data;
    }
}